//
//  WindowManager.swift
//  Window 管理
//
//  Created by mini on 2025/4/28.
//

import Foundation

class WindowManager {

    // MARK: -  单例
    static let shared = WindowManager()
    private init() {}

    // MARK: - 获取当前的 window
    func getCurrentWindow() -> UIWindow? {
        if #available(iOS 13.0, *) {
            if #available(iOS 15.0, *) {
                // iOS 15 及以上版本
                guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
                    return nil
                }
                return scene.windows.first(where: { $0.isKeyWindow })
            } else {
                // iOS 13-14 版本
                guard let scene = UIApplication.shared.connectedScenes
                    .first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
                else {
                    return nil
                }
                return scene.windows.first(where: { $0.isKeyWindow })
            }
        } else {
            // iOS 13 以下版本
            return UIApplication.shared.windows.first(where: { $0.isKeyWindow })
        }
    }
}
